local nl = require'luaNumeric'
require 'luaFDTD3D'
local plot = require "Q2DViewerEx"

----------------------------------------------------------------------------------------------------------------------------------------
--初始化参数
local PointsProbePostView={
	funs = {};
	xAxis = 0;
	ytype = 1;
	xtype = 1;
};
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--创建表和创建元表
function PointsProbePostView:new (o)
	o = o or {};
	self.__index = self;
	setmetatable(o,self)
	o:init();
	return o;
end
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--创建表和创建元表
function PointsProbePostView:init ()
	self.funs = {};
	self.funs.abs = function(input) return nl.abs(input); end;
	self.funs.phs = function(input) return nl.arg(input)*180/pi; end;
	self.funs.real = function(input) return nl.real(input); end;
	self.funs.imag = function(input) return nl.imag(input); end;
	self.funs.log = function(input) 
		local  d = nl.abs(input);		
		for k,v in pairs(d) do 
			if d[k] < 1e-200 then d[k] = 1e-200; end
		end
		d = 20* nl.log10(d)
		return d; 
	end;
	self.fun_names = {};
	self.fun_names[1] = 'log';
	self.fun_names[2] = 'abs';
	self.fun_names[3] = 'phs';
	self.fun_names[4] = 'real';
	self.fun_names[5] = 'imag';	
	self.ytype = 1;
end
----------------------------------------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------------------------------------
--更新数据
function PointsProbePostView:refresh_figs ()
	print( 'refresh:',self.probe:name() )
	self.probe:update();
	self.results = self.probe:results();
	self:plot()
end
----------------------------------------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------------------------------------
--画图
function PointsProbePostView:plot ()
	print( 'plot:',self.probe:name() )
	self.view:clearGraphs();
	local x = self.results.freq;
	if self.xtype ==2 then
		x = self.results.lambda;
	end
	local fun = self.funs[ self.fun_names[self.ytype] ];
	for k,v in pairs(self.results.data ) do
		assert( #x == #self.results.data[k], '数据长度不匹配,请检查Probe设置或者重新计算')
		if #x == #self.results.data[k] then
		    self.view:addGraph(x,  fun( self.results.data[k] ), k );
		else
		    print( "data size dismatch:" , #x,  #self.results.data[k] );
		end
	end
	self.view:rescaleAxes();
	self:set_axis();
end
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--画图
function PointsProbePostView:rescale ()
	self:update_parameters_from_ui();
	self:plot();
end
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--坐标设置
function PointsProbePostView:set_axis()
	if self.xtype ==2 then
		self.view.xAxis:setProperty( {name="波长 ["..Geometry.ref_units_dim_str()..']'} )
	else
		self.view.xAxis:setProperty( {name="频率 ["..Geometry.ref_units_freq_str()..']'} )
	end
	
	if self.ytype ==1 then
		self.view.yAxis:setProperty( {name="S parameter [dB]"} )
	elseif self.ytype ==2 then
		self.view.yAxis:setProperty( {name="S parameter [Mag.]"} )
	elseif self.ytype ==3 then
		self.view.yAxis:setProperty( {name="Argument of S parameter [arc.]"} )
	elseif self.ytype ==4 then
		self.view.yAxis:setProperty( {name="Real part of S parameter"} )
	elseif self.ytype ==5 then
		self.view.yAxis:setProperty( {name="Imag part of S parameter"} )
	end
	 self.view:rescaleAxes();
end
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--导出
function PointsProbePostView:export_data (path)
	print(path);
	self.probe:save(path);
end
----------------------------------------------------------------------------------------------------------------------------------------

return PointsProbePostView;
